package com.caiwangdai.app.extension

import android.graphics.Typeface
import android.support.annotation.ColorRes
import android.support.v4.content.ContextCompat
import android.text.*
import android.text.method.LinkMovementMethod
import android.text.style.AbsoluteSizeSpan
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.view.ViewGroup
import android.widget.TextView

//TextView 扩展函数&扩展属性
//******    SpannableString     ******

//普通染色
fun TextView.dye(start: Int, end: Int, @ColorRes color: Int) {
    val origin = this.text.toString().trim { it <= ' ' }
    val str = SpannableString(origin)
    str.setSpan(ForegroundColorSpan(ContextCompat.getColor(context, color)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)//染色
    text = str
}

//染色-Html
fun TextView.dyeByHtml(color: Int) {
    val str = "我是谁？我在哪？ <font color=${ContextCompat.getColor(context, color)}>我在干什么？</font>"
    text = Html.fromHtml(str)
}

//根据关键词染色，懒得再数了
fun TextView.dyeByKeyword(keyWord: String, @ColorRes color: Int) {
    val str = SpannableStringBuilder(text)
    var start = -1
    start = text.indexOf(keyWord)
    while (true) {
        start = text.indexOf(keyWord, start)
        if (start < 0 || start >= text.length) break
        str.setSpan(ForegroundColorSpan(ContextCompat.getColor(context, color)), start, start + keyWord.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)//染色
        start += keyWord.length
    }
    text = str
}

//染色+点击事件
fun TextView.dyeWithClick(start: Int, end: Int, @ColorRes color: Int, clickSpan: ClickableSpan) {
    val origin = text.toString().trim { it <= ' ' }
    val str = SpannableString(origin)
    str.setSpan(ForegroundColorSpan(ContextCompat.getColor(context, color)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)//染色
    str.setSpan(clickSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    text = str
    movementMethod = LinkMovementMethod.getInstance()//必须设置才能点击
    highlightColor = ContextCompat.getColor(context, android.R.color.transparent)//设置透明的高亮背景
}

abstract class BaseClickSpan : ClickableSpan() {

    override fun updateDrawState(ds: TextPaint?) {
        super.updateDrawState(ds)
        ds?.apply {
            isUnderlineText = false//没有下划线
        }
    }
}


//设置样式
val spanStyleBold = StyleSpan(Typeface.BOLD)
val spanStyleItalic = StyleSpan(Typeface.ITALIC)
fun TextView.setStyleSpan(start: Int, end: Int, styleSpan: Int) {
    val origin = text.toString().trim { it <= ' ' }
    val span = SpannableString(origin)
    span.setSpan(styleSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    text = span
}

//设置大小
fun TextView.setPartTextSize(start: Int, end: Int, textSize: Int) {
    //textsize 单位为dp
    val origin = text.toString().trim { it <= ' ' }
    val str = SpannableString(origin)
    str.setSpan(AbsoluteSizeSpan(textSize, true), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)//染色
    text = str
}


//******    Others     ******

var TextView.leftMargin: Int
    get():Int {
        return (layoutParams as ViewGroup.MarginLayoutParams).leftMargin
    }
    set(value) {
        (layoutParams as ViewGroup.MarginLayoutParams).leftMargin = value
    }

//针对TextView显示中文中出现的排版错乱问题，通过调用此方法得以解决
fun TextView.fullText(): String {
    val c = (text as String).toCharArray()
    for (i in c.indices) {
        if (c[i].toInt() == 12288) {
            c[i] = 32.toChar()
            continue
        }
        if (c[i].toInt() in 65281..65374) {
            c[i] = (c[i].toInt() - 65248).toChar()
        }

    }
    return String(c)
}


//顶部画drawable
fun TextView.setTopDrawable(recourse: Int) {
    val drawable = context.resources.getDrawable(recourse)
    drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight) //设置边界
    this.setCompoundDrawables(null, drawable, null, null)//画在顶部
}

